/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;
import java.awt.Font;
import java.util.Collection;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.Card;
import org.aergia.vinny.database.pojo.Skill;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.database.pojo.Volunteer;
import org.aergia.vinny.database.pojo.VolunteerGroup;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.DialogUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.util.TextField;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;
import org.aergia.vinny.util.DateUtil;

public class VolunteerEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;
	private TextField cardNumberField;
	private final VinnyController controller;
	private TextField firstNameField;
	private JComboBox groupNameField;
	private TextField lastNameField;
	private final VinnyUser user;
	private Volunteer volunteer;
	private JCheckBox volunteerActive;

	public VolunteerEditor(final VinnyController controller, final VinnyUser user) {
		this(controller, user, null);
	}

	/**
	 * @param controller
	 * @param volunteer
	 */
	public VolunteerEditor(final VinnyController controller, final VinnyUser user, final Volunteer volunteer) {
		this.controller = controller;
		this.user = user;
		this.volunteer = volunteer;
		this.initComponents();
	}

	@Override
	public String getTitle() {
		return "Volunteer Editor";
	}

	public Volunteer getVolunteer() {
		return this.volunteer;
	}

	@Override
	public boolean saveAction() {
		if (this.volunteer == null) {
			this.volunteer = new Volunteer();
		}

		if (!this.verify()) {
			return false;
		}

		this.volunteer.setActive(this.volunteerActive.isSelected());
		this.volunteer.setFirstName(this.firstNameField.getText());
		this.volunteer.setLastName(this.lastNameField.getText());

		final ApiResult<Card> cardResult = this.controller.isCardAvailable(this.volunteer, this.cardNumberField.getText());
		if (cardResult.isError()) {
			DialogUtil.alert(this, "Card already used, do you want to continue using this card", "Card already used");
			return false;
		}
		Card card = cardResult.getResult();
		if (card == null) {
			card = new Card(this.cardNumberField.getText(), this.volunteer, DateUtil.getInstance());
		}

		if ((this.volunteer.getCard() != null) && !this.volunteer.getCard().equals(card)) {
			this.controller.switchCard(this.volunteer.getCard(), card);
		}

		this.volunteer.setCard(card);
		this.volunteer.setGroup((VolunteerGroup) this.groupNameField.getSelectedItem());

		final Skill skill = this.controller.getSkill(Skill.GENERAL_SKILL).getResult();
		this.volunteer.addSkill(skill);

		this.saveVolunteer();
		return true;
	}

	private JTextField getCardNumberField() {
		if (this.cardNumberField == null) {
			this.cardNumberField = new TextField();
			this.cardNumberField.setName("Card Number");
			this.cardNumberField.setText((this.volunteer != null) && (this.volunteer.getCard() != null) ? this.volunteer.getCard().getNumber() : "Card");
		}
		return this.cardNumberField;
	}

	private JTextField getFirstNameField() {
		if (this.firstNameField == null) {
			this.firstNameField = new TextField();
			this.firstNameField.setName("First Name");
			this.firstNameField.setText((this.volunteer != null) && (this.volunteer.getFirstName() != null) ? this.volunteer.getFirstName() : "First Name");
		}
		return this.firstNameField;
	}

	private JComboBox getGroupNameField() {
		if (this.groupNameField == null) {
			final Collection<? extends VolunteerGroup> groups = this.controller.getGroups();
			this.groupNameField = new JComboBox(groups.toArray());
			this.groupNameField.setName("Group Name");
			if ((this.volunteer != null) && (this.volunteer.getGroup() != null)) {
				this.groupNameField.setSelectedItem(this.volunteer.getGroup());
			}
			this.groupNameField.setEditable(true);
		}
		return this.groupNameField;
	}

	private JTextField getLastNameField() {
		if (this.lastNameField == null) {
			this.lastNameField = new TextField();
			this.lastNameField.setName("Last Name");
			this.lastNameField.setText(this.volunteer != null ? this.volunteer.getLastName() : "Last Name");
		}
		return this.lastNameField;
	}

	private JCheckBox getVolunteerActive() {
		if (this.volunteerActive == null) {
			this.volunteerActive = new JCheckBox();
			this.volunteerActive.setName("Active?");
			this.volunteerActive.setSelected(this.volunteer != null ? this.volunteer.isActive() : true);
		}
		return this.volunteerActive;
	}

	private void initComponents() {
		this.add(new LabelPanel(new Font("Dialog", Font.PLAIN, 12), Color.black, this.getVolunteerActive(), this.getFirstNameField(), this.getLastNameField(),
				this.getCardNumberField(), this.getGroupNameField()));
		this.setVisible(true);
	}

	private void saveVolunteer() {
		final ApiResult<Volunteer> result = this.controller.saveVolunteer(this.volunteer, this.user);
		if (!result.isError()) {
			this.volunteer = result.getResult();
		}
	}

	private boolean verify() {
		if (this.firstNameField.getText().equals("First Name") || this.firstNameField.getText().isEmpty()) {
			DialogUtil.alert(this, "Please enter the volunteer's first name.", "Invalid first name");
			return false;
		}
		if (this.lastNameField.getText().equals("Last Name") || this.lastNameField.getText().isEmpty()) {
			DialogUtil.alert(this, "Please enter the volunteer's last name.", "Invalid last name");
			return false;
		}

		if (this.controller.isVinnyCard(this.cardNumberField.getText())) {
			final ApiResult<Card> cardResult = this.controller.isCardAvailable(this.volunteer, this.cardNumberField.getText());
			if (cardResult.isError()) {
				DialogUtil.alert(this, "The card " + cardResult.getText() + ".", "Card already in use.");
				return false;
			}
		} else {
			DialogUtil.alert(this, this.cardNumberField.getText() + " is not a valid card number.", "Invalid card");
			return false;
		}
		if (this.groupNameField.getSelectedItem() == null) {
			DialogUtil.alert(this, "Please enter the volunteer's group name.", "Invalid group name");
			return false;
		}

		if (this.groupNameField.getSelectedItem() instanceof String) {
			final String groupName = (String) this.groupNameField.getSelectedItem();
			ApiResult<VolunteerGroup> groupResult = this.controller.getGroup(groupName);
			if (groupResult.isError()) {
				groupResult = this.controller.addGroup(groupName, this.user);
				if (groupResult.isError()) {
					DialogUtil.alert(this, groupResult.getText(), "Error");
					return false;
				}
				this.groupNameField.remove(0);
				this.groupNameField.addItem(groupResult.getResult());
				this.groupNameField.setSelectedItem(groupResult.getResult());

			} else {
				this.groupNameField.setSelectedItem(groupResult.getResult());
			}

		}

		return true;

	}
}
